﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Brilliant.ORM
{
    /// <summary>
    /// SqlMap类
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    public class SqlMap<T> where T : EntityBase, new()
    {
        private List<SQL> sqlList;

        /// <summary>
        /// 默认构造器
        /// </summary>
        private SqlMap()
        {
            this.sqlList = new List<SQL>();
        }

        /// <summary>
        /// 带参构造器
        /// </summary>
        /// <param name="sql">SQL对象</param>
        private SqlMap(SQL sql)
        {
            this.sqlList = new List<SQL>();
            this.sqlList.Add(sql);
        }

        /// <summary>
        /// 带参构造器
        /// </summary>
        /// <param name="sqlList">SQL对象列表</param>
        private SqlMap(List<SQL> sqlList)
        {
            this.sqlList = sqlList;
        }

        /// <summary>
        /// 解析查询语句返回当前对象实例
        /// </summary>
        /// <param name="sql">查询语句参数</param>
        /// <returns>当前对象实例</returns>
        public static SqlMap<T> ParseSql(SQL sql)
        {
            SqlMap<T> instance = new SqlMap<T>(sql);
            return instance;
        }

        /// <summary>
        /// 解析查询语句返回当前对象实例
        /// </summary>
        /// <param name="sqlList">查询语句参数列表</param>
        /// <returns>当前对象实例</returns>
        public static SqlMap<T> ParseSql(List<SQL> sqlList)
        {
            SqlMap<T> instance = new SqlMap<T>(sqlList);
            return instance;
        }

        /// <summary>
        /// 获取查询结果
        /// </summary>
        /// <returns>查询结果</returns>
        private DataTable GetResult()
        {
            if (sqlList.Count <= 0)
            {
                throw new Exception("没有需要执行的SQL语句");
            }
            DataSet ds = DBHelper.DataProvider.ExecDataSet(sqlList[0]);
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 从查询结果中获取对象
        /// </summary>
        /// <param name="dtResult">查询结果</param>
        /// <returns>对象</returns>
        private T GetObject(DataTable dtResult)
        {
            if (dtResult == null || dtResult.Rows.Count <= 0)
            {
                return null;
            }
            if (dtResult.Rows.Count > 1)
            {
                throw new Exception("查询的结果不唯一");
            }
            T entity = new T();
            foreach (DataRow row in dtResult.Rows)
            {
                foreach (DataColumn col in dtResult.Columns)
                {
                    entity.SetProperty(col.ColumnName.ToLower(), row[col.ColumnName]);
                }
            }
            return entity;
        }

        /// <summary>
        /// 从查询结果中获取对象集合
        /// </summary>
        /// <param name="dtResult">查询结果</param>
        /// <returns>对象集合</returns>
        private List<T> GetList(DataTable dtResult)
        {
            List<T> list = new List<T>();
            if (dtResult == null || dtResult.Rows.Count <= 0)
            {
                return list;
            }
            foreach (DataRow row in dtResult.Rows)
            {
                T entity = new T();
                foreach (DataColumn col in dtResult.Columns)
                {
                    entity.SetProperty(col.ColumnName.ToLower(), row[col.ColumnName]);
                }
                list.Add(entity);
            }
            return list;
        }

        /// <summary>
        /// 将执行结果转换为对象
        /// </summary>
        /// <returns>对象</returns>
        public T ToObject()
        {
            DataTable dtResult = GetResult();
            return GetObject(dtResult);
        }

        /// <summary>
        /// 将执行结果转换为对象集合
        /// </summary>
        /// <returns>对象集合</returns>
        public List<T> ToList()
        {
            DataTable dtResult = GetResult();
            return GetList(dtResult);
        }

        /// <summary>
        /// 将执行结果转换为Json对象
        /// </summary>
        /// <returns>Json对象</returns>
        public string ToJsonObject()
        {
            DataTable dtResult = GetResult();
            T entity = GetObject(dtResult);
            return JsonSerializer.SerializeToJson<T>(entity);
        }

        /// <summary>
        /// 将执行结果转换为Json对象列表
        /// </summary>
        /// <returns>Json对象列表</returns>
        public string ToJsonList()
        {
            DataTable dtResult = GetResult();
            List<T> list = GetList(dtResult);
            return JsonSerializer.SerializeToJson<List<T>>(list);
        }

        /// <summary>
        /// 执行查询语句返回受影响行数
        /// </summary>
        /// <returns>受影响行数</returns>
        public int Execute()
        {
            if (DBHelper.DataProvider.Transaction)
            {
                int count = 0;
                foreach (SQL sql in sqlList)
                {
                    count += DBHelper.DataProvider.ExecNonQuerry(sql);
                }
                return count;
            }
            return DBHelper.DataProvider.ExecTrans(sqlList);
        }

        /// <summary>
        /// 执行查询语句返回第一行第一列的值
        /// </summary>
        /// <returns>第一行第一列的值</returns>
        public object First()
        {
            return DBHelper.DataProvider.ExecScalar(sqlList[0]);
        }
    }
}
